package com.wn.controller;

import com.auth0.jwt.interfaces.Claim;
import com.wn.annotation.RequireRoles;
import com.wn.entity.Appraise;
import com.wn.service.AppraiseService;
import com.wn.utils.JwtUtil;
import com.wn.utils.PageParam;
import com.wn.utils.Rsp;
import com.wn.utils.SystemControllerLog;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import java.util.Map;

import static com.wn.utils.JwtUtil.parseToken;

/**
 * (Appraise)表控制层
 *
 * @author makejava
 * @since 2020-11-05 11:50:48
 */
@RestController
@RequestMapping("appraise")
public class AppraiseController {
    /**
     * 服务对象
     */
    @Resource
    private AppraiseService appraiseService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Appraise selectOne(String id) {
        return this.appraiseService.queryById(id);
    }

    //普通用户
    @PostMapping("/searchSomeAppraiseByPage")
    @ApiOperation("查询所有用户 支持 多条件 分页 排序 查询")
    public Rsp searchSomeByPage(@RequestBody PageParam<Appraise> appraisePageParam,@RequestHeader("token") String token){

        Appraise appraise=new Appraise();
        String userId = JwtUtil.parseToken(token).get("id").asString();
        appraise.setCreateuserid(userId);
        appraise.setIsdeleted(0);
        appraisePageParam.setModel(appraise);
        return Rsp.success(appraiseService.queryAllByLimit(appraisePageParam));
    }

    //管理员用
    @PostMapping("/searchAppraiseByPage")
    @RequireRoles(roles = {"admin","super_admin","reception"},index = 1)
    @ApiOperation("查询所有用户 支持 多条件 分页 排序 查询")
    public Rsp searchByPage(@RequestBody PageParam<Appraise> appraisePageParam,@RequestHeader("token") String token){


        return Rsp.success(appraiseService.queryAllByLimit(appraisePageParam));
    }


    @PostMapping("/addOneAppraise")
    public Rsp addOne(@RequestBody  Appraise appraise,@RequestHeader("token") String token){

        String userId = JwtUtil.parseToken(token).get("id").asString();
        appraise.setCreateuserid(userId);
        return appraiseService.insert(appraise);
    }

    @PostMapping("/deleteAppraiseLogical")
    public Rsp deleteUserLogical(@RequestBody Appraise appraise){

        return appraiseService.deleteLogical(appraise);
    }


    @PostMapping("/updateAppraise")
    public Rsp updateUser(@RequestBody Appraise appraise){

        return appraiseService.update(appraise);
    }
}
